package com.xindaoapp.happypet.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.wpa.WPA;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.BigBanner;
import com.xindaoapp.happypet.bean.MoreProduct;
import com.xindaoapp.happypet.bean.PriceLadder;
import com.xindaoapp.happypet.bean.Product;
import com.xindaoapp.happypet.bean.ProductEntity;
import com.xindaoapp.happypet.bean.SmallBanner;
import com.xindaoapp.happypet.fragments.ShoppingCartFragment;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.MyScrollView;
import com.xindaoapp.share.SharePopupWindow;

/**
 * 商品详情
 * 
 * @author Administrator 1 团购 2 秒杀
 */
public class ProductDetailActivity1 extends BaseLoadAnimationAcitivity implements OnClickListener {
    private Activity mContext;
    @ViewInject(R.id.viewpager)
    // 轮播
    private ViewPager viewpager;
    @ViewInject(R.id.product_detail_size)
    // 广告当前页
    private TextView size;
    @ViewInject(R.id.product_detail_size_total)
    // 广告业总数
    private TextView total;

    // 商品信息
    @ViewInject(R.id.product_detail_title)
    // 商品名称
    private TextView tittle;
    @ViewInject(R.id.product_detail_sale_out)
    // 还剩
    private TextView sale;
    @ViewInject(R.id.product_detail_sale_in)
    // 剩余
    private TextView have;
    @ViewInject(R.id.product_detail_vip_price)
    // vip价格
    private TextView vip_price;
    @ViewInject(R.id.product_detail_normal_price)
    // 市场格
    private TextView normal_price;

    @ViewInject(R.id.product_detail_cart)
    // 购物车
    private FrameLayout cart;
    @ViewInject(R.id.product_detail_cart_size)
    // 购物车商品数量
    private TextView cartsize;

    @ViewInject(R.id.pro_guige)
    // 规格
    private RelativeLayout pro_guige;
    @ViewInject(R.id.pro_tuwen)
    // TUWEN
    private RelativeLayout pro_tuwen;
    @ViewInject(R.id.pro_info)
    // 详情
    private RelativeLayout pro_info;
    @ViewInject(R.id.pro_pingjia)
    // TUWEN
    private RelativeLayout pro_pingjia;

    @ViewInject(R.id.pingjia_total)
    // 评价数量
    private TextView pingjia_total;
    @ViewInject(R.id.pingjia_star)
    // 评价数量
    private LinearLayout pingjia_star;

    @ViewInject(R.id.add_cart)
    // 加入购物车
    private Button add_cart;

    @ViewInject(R.id.contact_service)
    // 联系客服
    private Button contact_service;

    @ViewInject(R.id.attention)
    // 关注
    private RelativeLayout attention;

    @ViewInject(R.id.product_detail_more)
    // 更多布局
    private LinearLayout more;
    @ViewInject(R.id.l_buy_more)
    // 更多商品
    private LinearLayout product_detail_more;

    private Product mProduct;

    private String productId;

    @ViewInject(R.id.no_result)
    private RelativeLayout no_result;

    @ViewInject(R.id.back)
    private ImageView back;
    @ViewInject(R.id.share)
    private ImageView share;

    @ViewInject(R.id.iv4)
    private ImageView iv4;

    @ViewInject(R.id.scrollview)
    private MyScrollView scrollview;

    private String isKill;

    private ProductDetailActivityBroadCast broadCast;

    @ViewInject(R.id.ll_picsize)
    private LinearLayout ll_picsize;

    @ViewInject(R.id.arrow)
    private ImageView arrow;

    @ViewInject(R.id.tv4)
    private TextView tv4;

    private SharePopupWindow win;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_productdetail1);
        super.onCreate(savedInstanceState);
        productId = getIntent().getStringExtra("productId");
        if (!TextUtils.isEmpty(getIntent().getStringExtra("isKill"))) {
            isKill = getIntent().getStringExtra("isKill");
        }
        ViewUtils.inject(this);
        mContext = this;
        getData();
        initOnClick();
        broadCast = new ProductDetailActivityBroadCast();
        registerReceiver(broadCast, new IntentFilter(Constants.DELETE_CART));
    }

    private void initOnClick() {
        pro_guige.setOnClickListener(this);
        pro_tuwen.setOnClickListener(this);
        pro_info.setOnClickListener(this);
        pro_pingjia.setOnClickListener(this);
        cart.setOnClickListener(this);
        add_cart.setOnClickListener(this);
        attention.setOnClickListener(this);
        back.setOnClickListener(this);
        share.setOnClickListener(this);
        contact_service.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pro_info:
                if (mProduct != null) {
                    Intent inforIntent =
                            new Intent(ProductDetailActivity1.this, ProductInforActivity.class);
                    inforIntent.putExtra("pro", mProduct);
                    inforIntent.putExtra("productID", productId);
                    startActivity(inforIntent);
                } else {
                    Toast.makeText(getApplicationContext(), "获取商品信息失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.pro_pingjia:
                Intent intent = new Intent(mContext, CommentListActivity.class);
                intent.putExtra("productID", productId);
                startActivity(intent);
                break;
            case R.id.pro_guige:
                if (isKill == null) {
                    isKill = "1";
                }
                if (mProduct != null) {
                    Intent pro_guige = new Intent(mContext, ProductAddActivity.class);
                    pro_guige.putExtra("pro", mProduct);
                    pro_guige.putExtra("isKill", isKill);
                    startActivity(pro_guige);
                } else {
                    Toast.makeText(getApplicationContext(), "获取商品信息失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.product_detail_cart:
                if (UserState.isLogged(mContext)) {
                    Intent mainIntent = new Intent(ProductDetailActivity1.this, MainActivity.class);
                    mainIntent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            ShoppingCartFragment.class.getCanonicalName());
                    startActivity(mainIntent);
                }
                break;
            case R.id.add_cart:
                if (isKill == null) {
                    isKill = "1";
                }
                if (mProduct != null) {
                    Intent add_cart = new Intent(mContext, ProductAddActivity.class);
                    add_cart.putExtra("pro", mProduct);
                    add_cart.putExtra("isKill", isKill);
                    startActivity(add_cart);
                } else {
                    Toast.makeText(getApplicationContext(), "获取商品信息失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.nowbuy:
                if (isKill == null) {
                    isKill = "1";
                }
                Intent nowbuy = new Intent(mContext, ProductAddActivity.class);
                nowbuy.putExtra("pro", mProduct);
                nowbuy.putExtra("nowbuy", "1");
                nowbuy.putExtra("isKill", isKill);
                startActivity(nowbuy);
                break;
            case R.id.contact_service: // 联系客服
                String text = (String) tittle.getText();
                // QQAuth mqqAuth = QQAuth.createInstance("10021591", mContext);
                WPA mWPA = new WPA(this, HappyPetApplication.get().getQqAuth().getQQToken());
                String ESQ = "48522388";
                int ret = mWPA.startWPAConversation(ESQ, "你好，我正在尾巴圈看这个商品~\n" + text); // 客服QQ
                if (ret != 0) {
                    Toast.makeText(getApplicationContext(), "抱歉，联系客服出现了错误~. error:" + ret,
                            Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.attention:
            case R.id.product_detail_collect:// 收藏
                if (UserState.isLogged(mContext)) {
                    if (mProduct != null) {
                        getCollectData();
                    } else {
                        Toast.makeText(getApplicationContext(), "获取商品信息失败", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
                break;
            case R.id.back:
                finish();
                break;
            case R.id.share:
                String s = "http://ts.leepet.com/share/index.html?id=" + productId;
                win = new SharePopupWindow(picurl);
                win.setDefaultIconId(R.drawable.icon);
                win.setInfors(
                        this,
                        String.format("在 @尾巴圈客户端上发现这个商品很不错哦！“%s” %s", tittle.getText().toString(),
                                s), tittle.getText().toString(), goods_name, s).showAtLocation(
                        this.findViewById(R.id.contact_service), Gravity.BOTTOM, 0, 0);

                break;
            case R.id.pro_tuwen:
                Intent pro_tuwen = new Intent(getApplicationContext(), TuWenActivity.class);
                pro_tuwen.putExtra("id", productId);
                startActivity(pro_tuwen);
                break;
            default:
                break;
        }
    }

    private void getData() {
        if (CommonUtil.checkNetState(mContext)) {
            RequestParams params = new RequestParams();
            params.put("goodsID", productId);
            if (UserState.isLogged()) {
                params.put("userID", UserState.getUserUid());
            }
            AddClient.get(Address.PRODUCT_DETAIL, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    onDataArrived(true);
                    cart.setVisibility(View.VISIBLE);
                    ProductEntity entity = null;
                    try {
                        entity = JSON.parseObject(content, ProductEntity.class);
                        if (null == entity) {
                            no_result.setVisibility(View.VISIBLE);
                            scrollview.setVisibility(View.GONE);
                            no_result.setVisibility(View.VISIBLE);
                            cart.setVisibility(View.GONE);
                            return;
                        }
                        Product product = entity.data;
                        mProduct = entity.data;
                        if (null == product) {
                            no_result.setVisibility(View.VISIBLE);
                            scrollview.setVisibility(View.GONE);
                            no_result.setVisibility(View.VISIBLE);
                            cart.setVisibility(View.GONE);
                            return;
                        }
                        initMyData(product);

                        if (UserState.isLogged()) {
                            getCartNumber();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }


                @Override
                public void onFailure(Throwable error, String content) {
                    onDataArrived(false);
                    SetOnLoadFailListener(new OnLoadFailListener() {
                        @Override
                        public void onLoadFail() {
                            getData();
                        }
                    });
                    super.onFailure(error, content);
                }
            });
        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }

    private void getCartNumber() {
        if (CommonUtil.checkNetState(mContext)) {
            RequestParams params = new RequestParams();
            params.put("userID", CommonUtil.decode(UserState.getUserUid()));
            params.add("version", "1.0");
            AddClient.get(Address.GET_CARTNUMBER, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    String data = null;
                    JSONObject obj;
                    try {
                        obj = new JSONObject(content);
                        data = obj.getString("data");
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    String count = null;
                    JSONObject obj1;
                    try {
                        obj1 = new JSONObject(data);
                        count = obj1.getString("count");
                        if (Integer.parseInt(count) > 0) {
                            if (cartsize.getVisibility() == View.GONE) {
                                cartsize.setVisibility(View.VISIBLE);
                            }
                            cartsize.setText(count);
                        }
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Throwable error, String content) {
                    super.onFailure(error, content);
                }
            });
        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }

    private int goods_numnber;

    private String picurl;
    private String goods_name;


    private void initMyData(Product info) {
        mProduct = info;
        goods_name = info.goods_name;
        List<SmallBanner> small = info.thumb_url;// 小轮播图
        if (small != null && small.size() > 0) {
            picurl = small.get(0).url;
        }
        ArrayList<BigBanner> big = (ArrayList<BigBanner>) info.img_url;// 大轮播图
        List<PriceLadder> ladder = info.price_ladder;
        List<MoreProduct> more = info.introduce_goods;
        goods_numnber = info.goods_number;
        if (info.is_collection == 1) {
            attention.setSelected(true);
            tv4.setText("已关注");
        } else if (info.is_collection == 0) {
            attention.setSelected(false);
            tv4.setText("关注");
        }
        prepareViewPage(small, big);
        tittle.setText(info.goods_name);
        sale.setText("热度" + info.sold_number);
        have.setText("库存" + info.goods_number + "件");
        sale.setText(changeTextColor(sale, 2, info.sold_number.length()));
        have.setText(changeTextColor(have, 2, String.valueOf(info.goods_number).length()));
        if (info.types.equals("0")) {
            vip_price.setText("尾巴圈价:￥" + formatDaZhe(Float.valueOf(info.shop_price)));
        } else if (info.types.equals("1")) {
            vip_price.setText("团购价:￥" + formatDaZhe(Float.valueOf(info.price_ladder.get(0).price)));
        } else if (info.types.equals("2")) {
            vip_price.setText("秒杀价:￥" + formatDaZhe(Float.valueOf(info.price_ladder.get(0).price)));
            if (!TextUtils.isEmpty(isKill)) {
                if (isKill.equals("0")) {
                    vip_price.setText("尾巴圈价:￥" + formatDaZhe(Float.valueOf(info.shop_price)));
                } else {
                    vip_price.setText("秒杀价:￥"
                            + formatDaZhe(Float.valueOf(info.price_ladder.get(0).price)));
                }
            }
        }
        if (info.is_activity.equals("1")) {
            add_cart.setText("立即购买");
        } else if (info.is_activity.equals("0")) {
            int count = 0;
            if (info.about_attr != null && info.about_attr.size() > 0) {
                for (int i = 0; i < info.about_attr.size(); i++) {
                    count = count + Integer.parseInt(info.about_attr.get(i).is_activity);
                }
            }
            if (count > 0) {
                add_cart.setText("立即购买");
            }
        }
        normal_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);// 在文字中间画线
        normal_price.setText("￥" + info.market_price);

        prepareEvalStar(info.comment_star);
        pingjia_total.setText("(" + info.comment_count + ")");

        if (more.size() > 0) {
            prepareMore(more);
        } else {
            findViewById(R.id.l_buy_more).setVisibility(View.GONE);
        }

        if (Integer.valueOf(info.goods_number) == 0) {
            add_cart.setClickable(false);
            add_cart.setBackgroundResource(R.drawable.product_detail_addcart_p);
        }
    }

    private String formatDaZhe(Float a) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(a);
    }

    private void prepareEvalStar(int comment_star) {
        pingjia_star.removeAllViews();
        for (int i = 0; i < 5; i++) {
            LinearLayout.LayoutParams lp =
                    new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
            lp.leftMargin = 5;
            ImageView iv = new ImageView(mContext);
            if (i < comment_star) {
                iv.setBackgroundResource(R.drawable.star);
            } else {
                iv.setBackgroundResource(R.drawable.star_normal);
            }
            iv.setLayoutParams(lp);
            pingjia_star.addView(iv);
        }
    }

    private void prepareMore(final List<MoreProduct> info) {
        more.removeAllViews();
        for (int i = 0; i < info.size(); i++) {
            View view = View.inflate(mContext, R.layout.product_detail_more_data, null);
            ImageView iv = (ImageView) view.findViewById(R.id.more_img);
            TextView title = (TextView) view.findViewById(R.id.more_title);
            TextView price = (TextView) view.findViewById(R.id.more_price);

            int dip2px = CommonUtil.dip2px(mContext, 10);

            int width = getWidth();
            LinearLayout.LayoutParams lp =
                    new LinearLayout.LayoutParams((width - dip2px * 4) / 3,
                            (width - dip2px * 4) / 3);
            title.setWidth((width - dip2px * 4) / 3);
            ImageLoader.getInstance().displayImage(info.get(i).goods_thumb, iv);
            iv.setPadding(1, 1, 1, 1);
            iv.setBackgroundColor(Color.parseColor("#D1D0D4"));
            iv.setLayoutParams(lp);
            iv.setScaleType(ScaleType.FIT_XY);
            title.setText(info.get(i).goods_name);
            price.setText("￥" + formatDaZhe(Float.valueOf(info.get(i).shop_price)));
            more.addView(view);
            final int j = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    productId = info.get(j).goods_id;
                    Intent intent =
                            new Intent(ProductDetailActivity1.this, ProductDetailActivity1.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("productId", productId);
                    startActivity(intent);
                }
            });
        }
    }

    private int getWidth() {
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    private SpannableStringBuilder changeTextColor(TextView tv, int start, int end) {
        SpannableStringBuilder builder = new SpannableStringBuilder(tv.getText().toString());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.parseColor("#f03c13"));
        builder.setSpan(redSpan, start, start + end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }

    public void prepareViewPage(final List<SmallBanner> list, ArrayList<BigBanner> big) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(list, big);
        viewpager.setAdapter(adapter);

        viewpager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(final int position) {
                size.setText(position + 1 + "");
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {}

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {}
        });
        size.setText("1");
        total.setText(String.valueOf(list.size()));
        if (list == null || list.size() == 0) {
            ll_picsize.setVisibility(View.GONE);
        }
    }

    class ViewPagerAdapter extends PagerAdapter {
        private final List<SmallBanner> list;
        private final ArrayList<BigBanner> big;

        private ViewPagerAdapter(List<SmallBanner> list, ArrayList<BigBanner> big) {
            this.list = list;
            this.big = big;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        /** 判断出去的view是否等于进来的view 如果为true直接复用 */
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        /** 销毁预加载以外的view对象, 会把需要销毁的对象的索引位置传进来就是position */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        /** 创建一个view */
        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView iv = new ImageView(mContext);
            iv.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    android.view.ViewGroup.LayoutParams.FILL_PARENT));
            String uri = list.get(position).url;
            // goods_big_loading
            ImageLoader.getInstance().displayImage(uri, iv);
            container.addView(iv);
            iv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        ArrayList list1 = new ArrayList();
                        list1.add(big);
                        Intent intent = new Intent(mContext, BigViewPageActivity.class);
                        intent.putStringArrayListExtra("list", list1);
                        intent.putExtra("current", position);
                        startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });
            return iv;
        }
    }

    @Override
    protected void onResume() {
        if (UserState.isLogged()) {
            getCartNumber();
        }
        super.onResume();
    }

    private class ProductDetailActivityBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int goods_num = intent.getIntExtra("goods_number", -1);
            if (goods_num == -1) {
                return;
            } else {
                // int cart_num = Integer.parseInt(cart.getText().toString()) -
                // goods_num;
                if (goods_num > 0) {
                    cartsize.setText(goods_num + "");
                } else {
                    if (cartsize.getVisibility() == View.VISIBLE) {
                        cartsize.setText("0");
                        cartsize.setVisibility(View.GONE);
                    }
                }
            }
        }
    }

    private void getCollectData() {// 获取收藏数据
        if (CommonUtil.checkNetState(mContext)) {
            final RequestParams params = new RequestParams();
            params.put("userID", UserState.getUserUid());
            params.put("goodsID", productId);
            if (attention.isSelected()) {// 取消
                AddClient.get(Address.DEL_COLLECTION, params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        int result = -1;
                        JSONObject obj;
                        try {
                            obj = new JSONObject(content);
                            result = obj.getInt("status");

                            if (result == 1) {// 说明没有赞
                                Toast.makeText(mContext, "取消收藏成功", Toast.LENGTH_SHORT).show();
                                attention.setSelected(false);
                                tv4.setText("关注");
                                sendBroadcast(new Intent(Constants.SHOP_COLLECT));
                            } else {
                                Toast.makeText(mContext, "取消收藏失败", Toast.LENGTH_SHORT).show();
                                attention.setSelected(true);
                                tv4.setText("已关注");
                            }
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Throwable arg0, String arg1) {
                        super.onFailure(arg0, arg1);
                        Toast.makeText(mContext, R.string.get_error_data, 0).show();
                    }
                });
            } else {
                AddClient.get(Address.ADD_COLLECTION, params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        int result = -1;
                        JSONObject obj;
                        try {
                            obj = new JSONObject(content);
                            result = obj.getInt("status");
                            if (result == 1) {// 说明没有赞
                                Toast.makeText(mContext, "添加收藏成功", Toast.LENGTH_SHORT).show();
                                tv4.setText("已关注");
                                attention.setSelected(true);
                            } else {
                                Toast.makeText(mContext, "添加收藏失败", Toast.LENGTH_SHORT).show();
                                tv4.setText("关注");
                                attention.setSelected(false);
                            }
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Throwable arg0, String arg1) {
                        super.onFailure(arg0, arg1);
                        Toast.makeText(mContext, R.string.get_error_data, 0).show();
                    }
                });
            }

        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadCast);
    }

    @Override
    public void onBackPressed() {
        if (null != win && win.isShowing()) {
            win.dismiss();
        } else {
            super.onBackPressed();
        }
    }
}
